#include "mappanel.h"
#include <QDebug>
#include <QGraphicsDropShadowEffect>
#include <QPainter>
QPixmap getRoundRectPixmap( QPixmap srcPixMap, QRect rect, int radius )
{
    //不处理空数据或者错误数据
    if ( srcPixMap.isNull() )
    {
        qDebug() << "error";
        return srcPixMap;
    }

    int imageWidth  = rect.width();
    int imageHeight = rect.height();

    qDebug() << imageWidth << " " << imageHeight;

    //处理大尺寸的图片,保证图片显示区域完整
    QPixmap newPixMap = srcPixMap.scaled( imageWidth, ( imageHeight == 0 ? imageWidth : imageHeight ), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );

    QPixmap destImage( imageWidth, imageHeight );
    destImage.fill( "#433e3a" );
    QPainter painter( &destImage );
    // 抗锯齿
    painter.setRenderHints( QPainter::Antialiasing, true );
    // 图片平滑处理
    painter.setRenderHints( QPainter::SmoothPixmapTransform, true );
    // 将图片裁剪为圆角
    QPainterPath path;
    path.addRoundedRect( rect, radius, radius );
    painter.setClipPath( path );
    painter.drawPixmap( 0, 0, imageWidth, imageHeight, newPixMap );
    return destImage;
}

MapPanel::MapPanel( QWidget* parent ) : QWidget( parent )
{
    QLabel*      label  = new QLabel( this );
    QHBoxLayout* layout = new QHBoxLayout;
    m_GroupBox          = new QGroupBox( this );

    this->m_GroupBox->setStyleSheet( QString( "QGroupBox{border:0px solid #79716b;border-radius:6px;background:#433e3a;}" ) );
    m_GroupBox->setAttribute( Qt::WA_TranslucentBackground );

    //边框阴影效果
    //    QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect;
    //    effect->setBlurRadius( 10 );
    //    effect->setColor( Qt::lightGray );
    //    effect->setOffset( 0, 0 );
    //    m_GroupBox->setGraphicsEffect( effect );
    //    m_GroupBox->update();

    QImage  img( ":/resource/map.png" );
    QPixmap pm = getRoundRectPixmap( QPixmap::fromImage( img ), QRect( 0, 0, img.width(), img.height() ), 6 );
    label->setPixmap( pm );
    label->resize( img.width(), img.height() );

    QHBoxLayout* imgLay = new QHBoxLayout;
    imgLay->addWidget( label );
    //    imgLay->setContentsMargins( 2, 2, 2, 2 );
    this->m_GroupBox->setLayout( imgLay );

    layout->addWidget( this->m_GroupBox );
    layout->setContentsMargins( 0, 0, 0, 0 );
    this->setLayout( layout );
    this->setFixedSize( 600, img.height() );
}
